package ifrn.pdsd.android.extranet;

import ifrn.pdsd.android.model.Tarefa;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListarTarefasByFuncActivity extends ListActivity {
	
	public static final int MENU1 = Menu.FIRST;
	public static final int MENU2 = Menu.FIRST+1;
	
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String METHOD_NAME = "ListarTarefasByUsuario";
	private static final String SOAP_ACTION = "http://tempuri.org/ListarTarefasByUsuario";
	private static final String URL = LoginActivity.enderecoWS;
	private List<Tarefa> listaTarefas= new ArrayList<Tarefa>();
	
	private int idFuncionario = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listar_tarefas_by_funcionario);
		Intent intent = getIntent();
		this.idFuncionario = intent.getIntExtra("idFuncionario", 0);

		realizarBusca(this.idFuncionario);
	}
    
    public void realizarBusca(int id) {
    	/*List<Map<String, String>> lista = new ArrayList<Map<String, String>>();//////
*/    	try {
            SoapObject requisicao = new SoapObject(NAMESPACE, METHOD_NAME);
            requisicao.addProperty("id", id);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(requisicao);

            HttpTransportSE http = new HttpTransportSE(URL);
            http.call(SOAP_ACTION, envelope);
            
            SoapObject response = (SoapObject) envelope.getResponse();
            
			if (response.getPropertyCount()== 0) {
				Log.w("tamanho da lista", listaTarefas.size() + "");

				Toast.makeText(ListarTarefasByFuncActivity.this,
						"Nao existe tarefas neste funcionario!", Toast.LENGTH_LONG)
						.show();

				startActivity(new Intent(ListarTarefasByFuncActivity.this,
						ListarFuncActivity.class));
			}
			
            for(int i = 0; i < response.getPropertyCount(); i++){
            	SoapObject objSoap = (SoapObject) response.getProperty(i);
				
                Log.w("TUDO", objSoap.toString());
                Tarefa f = new Tarefa();
                f.setId(Integer.parseInt(objSoap.getProperty(0).toString()));
                f.setTituloTarefa(objSoap.getProperty(1).toString());
                f.setPorcentagem(Double.parseDouble(objSoap.getProperty(2).toString()));
                f.setTempoEstimado(Integer.parseInt(objSoap.getProperty(3).toString()));
                f.setTempoTotalTrabalho(Double.parseDouble(objSoap.getProperty(4).toString()));
                f.setDescricao(objSoap.getProperty(5).toString());
                f.setPrioridade(Integer.parseInt(objSoap.getProperty(10).toString()));
                f.setGrauTarefa(objSoap.getProperty(7).toString());
                
                
                Log.w("prioridade", objSoap.getProperty(10).toString());
                Log.w("grau tarefa", objSoap.getProperty(7).toString());
                
                listaTarefas.add(f);
            }
        } catch (Exception e) {
        	Log.w("Erro", e.toString());
            e.printStackTrace();
        }
    	
    	setListAdapter(new ArrayAdapter<Tarefa>(this, android.R.layout.simple_list_item_1, listaTarefas));
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Object o = this.getListAdapter().getItem(position);
    	Log.w("objeto clicado", o.toString());
    	Tarefa tarefaClicada = (Tarefa) o;
    	Log.w("Tarefa clicada", ""+tarefaClicada.getId());
    	
    	Intent intent = new Intent(this, DetalhamentoTarefaActivity.class);
    	intent.putExtra("idTarefa", tarefaClicada.getId());
    	intent.putExtra("tituloTarefa", tarefaClicada.getTituloTarefa());
    	intent.putExtra("porcentagemTarefa", tarefaClicada.getPorcentagem());
    	intent.putExtra("tempoEstimadoTarefa", tarefaClicada.getTempoEstimado());
    	intent.putExtra("tempoTrabalhadoTarefa", tarefaClicada.getTempoTotalTrabalho());
    	
    	intent.putExtra("grauTarefa", tarefaClicada.getGrauTarefa());
    	intent.putExtra("prioridadeTarefa", tarefaClicada.getPrioridade());
    	
    	intent.putExtra("idFuncionario", this.idFuncionario);
    	
    	startActivity(intent);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU1, 0, "Voltar");
		menu.add(0, MENU2, 0, "Inicio");
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU1:
			startActivity(new Intent(ListarTarefasByFuncActivity.this,
					ListarFuncActivity.class));
			return true;
		case MENU2:
			startActivity(new Intent(ListarTarefasByFuncActivity.this,
					MenuActivity.class));
			return true;
		}
		return false;
	}

}
